from pms.common.views import PostFormula
from info.utils.notice import notice_list


class API(PostFormula):
    def handle(self, req):
        notices = notice_list(self.user)
        if 'did' in req and req['did'] is not None:
            notices = notices.filter(did_id=req['did'])
        elif 'pid' in req and req['pid'] is not None:
            notices = notices.filter(pid_id=req['pid'])
        elif 'system' in req and req['system'] is True:
            notices = notices.filter(did=None, pid=None)
        return self.success_respond({
            'list': [{
                'id': notice.id,
                'title': notice.title,
                'sender': {
                    'id': notice.uid.id,
                    'name': notice.uid.name
                },
                'date_time': {
                    'year': notice.date_time.year,
                    'month': notice.date_time.month,
                    'day': notice.date_time.day,
                    'hour': notice.date_time.hour,
                    'minute': notice.date_time.minute,
                    'second': notice.date_time.second
                }
            } for notice in notices]
        })
